<template>
  <div class="bac">
    <div class="login">
      <form
        id="msgform"
        @submit.prevent="onSubmit"
        autocomplete="off"
        action="http://127.0.0.1:8080/login"
        method="post"
      >
        <p class="tit">账号登陆</p>
        <p>
          <input
            type="text"
            v-model="username"
            name="uname"
            placeholder="输入账号"
          >
        </p>
        <p>
          <input
            type="password"
            v-model="password"
            name="upwd"
            placeholder="输入密码"
          >
        </p>
        <p class="usertype">
          <input
            type="radio"
            checked
            value="用户"
            name="type"
            @click="changeuser"
          ><span class="yonghu">用户</span>
          <input
            type="radio"
            value="管理员"
            name="type"
            v-model="radio"
          ><span>管理员</span>
        </p>
        <p>
          <input
            class="sub"
            type="submit"
            value="登录"
          >
        </p>
      </form>
      <div>
        <div
          class="register"
          style="margin-bottom:5px"
          @click="change"
        >
          修改密码
        </div>
        <div
          class="register"
          @click="register"
        >
          注册新账号
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data: () => ({
    username: '',
    password: '',
    radio: '用户'
  }),
  created() {
    // const res = await this.$http.getusers()
    // console.log(res.message)
  },
  methods: {
    async login() {
      // await this.$http.login()
    },
    onSubmit: async function (e) {
      // 阻止默认表单提交
      // 做你自己想做的事，比如ajax请求后台数据
      console.log(this.radio)
      const userObj = {
        username: this.username,
        password: this.password
      }
      let res = []
      if (this.radio === '用户') {
        res = await this.$http.login(userObj)
      } else {
        res = await this.$http.adminlogin(userObj)
      }
      if (res.code === 200) {
        sessionStorage.setItem('token', this.username)
        sessionStorage.setItem('type', res.type)
        sessionStorage.setItem('user', JSON.stringify(res.user))
        this.$store.state.user = res.user
        this.radio === '用户' ? this.$router.push('/home') : this.$router.push('/admin')
        this.$message({
          message: '登陆成功',
          type: 'success'
        })
      } else {
        this.$message.error('账号或者密码错误')
      }
      e.preventDefault()
    },
    register() {
      this.$router.push('/register')
    },
    change() {
      this.$router.push('/change')
    },
    changeuser() {
      this.radio = '用户'
    }
  }
}
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.bac {
  position: absolute;
  width: 100vw;
  height: 100vh;
  background: url(../img/bac2.jpg) no-repeat center / 100% 100%;
}
.login {
  position: absolute;
  background: rgba(255, 255, 255, 0.89);
  right: 0;
  bottom: 0;
  top: 0;
  left: 0;
  height: 330px;
  margin: auto;
  width: 350px;
  text-align: center;
  padding-top: 30px;
  input::-webkit-input-placeholder {
    color: #bbb;
    font-weight: normal;
  }
  input {
    border: 1px solid #ccc;
    width: 300px;
    height: 42px;
    background: #fff;
    text-indent: 10px;
    margin: 10px 0;
    font-weight: bold;
    border-radius: 5px;
  }
  .usertype {
    input {
      height: 12px;
      width: 12px;
      line-height: 12px;
      position: relative;
      right: -93px;
      margin-right: 33px;
    }
    .yonghu {
      margin-right: 10px;
    }
    span {
      position: absolute;
      margin-left: 64px;
      line-height: 12px;
      top: 220px;
    }
  }
  .sub {
    font-size: 18px;
    color: #fff;
    background: #3b78dd;
    height: 50px;
    margin-bottom: 10px;
    cursor: pointer;
  }
  .register {
    cursor: pointer;
  }
  .tit {
    margin-bottom: 20px;
    font-size: 24px;
    font-weight: bold;
  }
}
</style>
